package com.dmall.play.interview.chapter07;

public class Leetcode404 {

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int val) {
            this.val = val;
        }
    }

    public int sumOfLeftLeaves(TreeNode root) {
        return sumOfLeftLeaves(root, false);
    }

    private int sumOfLeftLeaves(TreeNode node, boolean left) {
        if (node == null) {
            return 0;
        }
        if (left && node.left == null && node.right == null) {
            return node.val;
        }
        return sumOfLeftLeaves(node.left, true) + sumOfLeftLeaves(node.right, false);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(9);
        root.right = new TreeNode(20);
        root.right.left = new TreeNode(15);
        root.right.right = new TreeNode(7);
        Leetcode404 leetcode404 = new Leetcode404();
        System.out.println(leetcode404.sumOfLeftLeaves(root));
    }
}
